package com.toolset.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.toolset.Bean.User;
import com.toolset.R;

import org.litepal.crud.DataSupport;

import java.util.List;

public class RegisterActivity extends BaseActivity {
    private EditText userId;
    private EditText userPassword;
    private EditText confirmPassword;
    private EditText userName;
    private EditText userEmail;
    private RadioButton man;
    private RadioButton woman;
    private Button registerBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        //初始化事件
        initEvent();

        //注册
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //检查信息通过则判断是否已有
                if (checkData()){
                    //查询该Id 是否在数据库中存在
                     List<User> users = (List<User>) DataSupport.where("userId = ?",userId.getText().toString()).find(User.class);
                    if(users.size()>0){
                        Toast.makeText(RegisterActivity.this,"账号："+userId.getText().toString()+"已存在！",Toast.LENGTH_SHORT).show();
                    }else{
                        //不存在则插入数据
                        User user = new User();
                        user.setUserId(userId.getText().toString());
                        user.setUserPassword(userPassword.getText().toString());
                        user.setUserName(userName.getText().toString());
                        user.setUserMail(userEmail.getText().toString());

                        //判断选中性别
                        if(man.isChecked()){
                            user.setUserSex(man.getText().toString());
                        }else{
                            user.setUserSex(woman.getText().toString());
                        }

                        //插入数据
                        if(user.save()){
                            Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(RegisterActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                        }


                    }
                }
            }
        });

    }

    //初始化事件
    private  void initEvent(){
        userId = (EditText) findViewById(R.id.registerUserId);
        userPassword = (EditText) findViewById(R.id.registerUserPassword);
        confirmPassword = (EditText) findViewById(R.id.registerConfirmUserPassword);
        userName = (EditText) findViewById(R.id.registerUserName);
        userEmail = (EditText) findViewById(R.id.registerUserEmail);
        man = (RadioButton) findViewById(R.id.registerMan);
        woman = (RadioButton) findViewById(R.id.registerWoman);
        registerBtn = (Button) findViewById(R.id.registerBtn);
    }

    //检查信息是否匹配
    private boolean checkData(){


        if(userId.getText().length()==0){
            Toast.makeText(RegisterActivity.this,"账号不能为空！请重新输入~",Toast.LENGTH_SHORT).show();
            return false;
        }

        if(userPassword.getText().length()==0){
            Toast.makeText(RegisterActivity.this,"密码不能为空！请重新输入~",Toast.LENGTH_SHORT).show();
            return false;
        }

        if(confirmPassword.getText().length()==0){
            Toast.makeText(RegisterActivity.this,"确认密码不能为空！请再次输入确认密码~",Toast.LENGTH_SHORT).show();
            return false;
        }

        if(!confirmPassword.getText().toString().equals(userPassword.getText().toString())){
            Toast.makeText(RegisterActivity.this,"两次密码不一致！请检查密码~",Toast.LENGTH_SHORT).show();
            return false;
        }

        if(userName.getText().length()==0){
            Toast.makeText(RegisterActivity.this,"姓名不能为空！请重新输入~",Toast.LENGTH_SHORT).show();
            return false;
        }

        //邮箱正则表达式
        // “名称@域名”，需要使用“^”匹配邮箱的开始部分，
        // 用“$”匹配邮箱结束部分以保证邮箱前后不能有其他字符，
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";

        String mail = userEmail.getText().toString();
        if (!mail.matches(regex)) {
            Toast.makeText(RegisterActivity.this,"邮箱格式错误！请重新输入~",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

}
